var app = new Vue({
    el: '.Promotionpage',
    data() {
        return {
            location: '',
            theuser:'',
            information:'',
            HPS: "HPS",
            ETH: "ETH",
            BTC: "BTC",
            HSR: "HSR",
            HAND: "HAND",
            HART: "HART",
            MOT: "MOT",
            brokerage0:'',
            brokerage1:'',
            brokerage2:'',
            brokerage3:'',
            brokerage4:'',
            brokerage5:'',
            brokerage6:'',
            spreadnum: 0, //我的推广/推广代码
            myinvitation:{ //推广用户列表
                p:1, //当前页数
                s:8, //每页条数
                total:null,//总条数
                list:[], //当前页数返回数组
                showpage:5 //默认显示页数5个页
            },
            usercommision:{ //推广详情
                coin:'', //币种
                p:1, //当前页数
                s:8, //每页条数
                total:null,//总条数
                list:[], //当前页数返回数组
                showpage:5, //默认显示页数5个页
                brokerage:null
            },
            loading:false
        };
    },
    created(){
        this.location = location.href.split('/')[0]+'//'+location.href.split('/')[2]+'/'
        let id =window.localStorage.getItem("ID")
        let ref=this.location+ "m?ref=" +id
        window.localStorage.setItem('ref',ref)
        // document.getElementsByClassName('errorMessage')[0].style.display="none"
        // document.getElementsByClassName('maskLayer')[0].style.display="none"
        let params2={
            p:this.usercommision.p,
            s:this.usercommision.s
        }
        this.getCommision(params2)
        let params={
            p:this.myinvitation.p,
            s:this.myinvitation.s
        }
        this.getInvitations(params)
        
    },
    mounted(){
        this.loading=true
    },
    computed: {
        lastPage: function () {
            var pag = [];
            if (this.Quantity < this.Afew) { //如果当前的激活的项 小于要显示的条数
                //总页数和要显示的条数那个大就显示多少条
                var i = Math.min(this.Afew, this.Pagenumber);
                while (i) {
                    pag.unshift(i--);
                }
            } else { //当前页数大于显示页数了
                var middle = this.Quantity - Math.floor(this.Afew / 2),//从哪里开始
                    i = this.Afew;
                if (middle > (this.Pagenumber - this.Afew)) {
                    middle = (this.Pagenumber - this.Afew) + 1
                }
                while (i--) {
                    pag.push(middle++);
                }
            }
            return pag
        },
        pages: function () {
            var pag = [];
            if (this.coinhistory.p < this.showItem) { //如果当前的激活的项 小于要显示的条数
                //总页数和要显示的条数那个大就显示多少条
                var i = Math.min(this.showItem, this.allpage);
                while (i) {
                    pag.unshift(i--);
                }
            } else { //当前页数大于显示页数了
                var middle = this.coinhistory.p - Math.floor(this.showItem / 2),//从哪里开始
                    i = this.showItem;
                if (middle > (this.allpage - this.showItem)) {
                    middle = (this.allpage - this.showItem) + 1
                }
                while (i--) {
                    pag.push(middle++);
                }
            }
            return pag
        },
        invitationpages(){ //推广用户页数列表
            let pag=[]
            if(this.myinvitation.p<this.myinvitation.showpage){ //当前选择页数小于默认页数
                // let i=Math.min(this.myinvitation.showpage,parseInt(this.myinvitation.total))
                let i=Math.ceil(parseInt(this.myinvitation.total)/this.myinvitation.s)
                while(i){
                    pag.unshift(i--)
                }
            }else{//当前选择页数大于等于默认页数
                let middle=this.myinvitation.p-Math.floor(this.myinvitation.showpage/2) //设置起始页数
                let i=this.myinvitation.showpage 
                if(middle>(parseInt(this.myinvitation.total)-this.myinvitation.showpage)){
                    middle=(parseInt(this.myinvitation.total)-this.myinvitation.showpage)+1
                }
                while (i--) {
                    pag.push(middle++)
                }
            }  
            return pag
        },
        usercommisionpages(){ //推广用户页数列表
            let pag=[]
            if(this.usercommision.p<this.usercommision.showpage){ //当前选择页数小于默认页数
                // let i=Math.min(this.myinvitation.showpage,parseInt(this.myinvitation.total))
                let i=Math.ceil(parseInt(this.usercommision.total)/this.usercommision.s)
                while(i){
                    pag.unshift(i--)
                }
            }else{//当前选择页数大于等于默认页数
                let middle=this.usercommision.p-Math.floor(this.usercommision.showpage/2) //设置起始页数
                let i=this.usercommision.showpage 
                if(middle>(parseInt(this.usercommision.total)-this.usercommision.showpage)){
                    middle=(parseInt(this.usercommision.total)-this.usercommision.showpage)+1
                }
                while (i--) {
                    pag.push(middle++)
                }
            }  
            return pag
        },
        onUpload(e) {
            var formData = new FormData();
            formData.append('file', e.target.files[0]);
        },
    },
    methods: {
        getCgera: function (num) {
            if(parseFloat(num)>10000){
                return parseFloat(parseFloat(num).toFixed(4))
            }else if(parseFloat(num)>100){
                return parseFloat(parseFloat(num).toFixed(6))
            }else if(parseFloat(num)>0){
                return parseFloat(parseFloat(num).toFixed(10))
            }else if(parseFloat(num)==0){
                return parseFloat(num).toFixed(0)
            }else{
                return parseFloat(num).toFixed(0)
            }
        },
        webhref(imgstr){ // 获取推广代码
            // let userid = this.locaId ? this.locaId : this.personal.id
            let userid = window.localStorage.getItem('ID')
            let html = `<a href="${this.location}?ref=${userid}"><img src="${imgstr}"/></a>`
            return html   
        },
        getCookie(cookiename) { //获取cookie
            var arr,
              reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
            if ((arr = document.cookie.match(reg))) {
              return unescape(arr[2]);
            } else {
              return null;
            }
        },
        closeinfo() {
            document.getElementsByClassName('errorMessage')[0].style.display="none"
            this.theuser = "";
            this.information = ""
            let BODY = document.getElementsByTagName("body")[0]
            BODY.style.overflowY = ""
        },
        onCopy(coin){ //复制充值地址
            let lang = this.getCookie("lang");
        if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { //区分iPhone设备  
            window.getSelection().removeAllRanges(); //这段代码必须放在前面否则无效  
            var Url2 = document.getElementById("biaoios"); //要复制文字的节点  
            var range = document.createRange();
            // 选中需要复制的节点  
            range.selectNode(Url2);
            // 执行选中元素  
            window.getSelection().addRange(range);
            // 执行 copy 操作  
            var successful = document.execCommand('Copy');
            // 移除选中的元素  
            window.getSelection().removeAllRanges();
            document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
            if (lang == "en_US") {
                this.theuser = "Successfully copied"
                this.information = "Successfully copied"
            } else if (lang == "zh_TW") {
                this.theuser = "複製成功"
                this.information = "複製成功"
            } else {
                this.theuser = "复制成功"
                this.information = "复制成功"
            }
            return;
          } else {
            var Url2 = document.getElementById("biaoios"); //要复制文字的节点  
            var input = $('.input')
            input.html(Url2)
            input.select();
            document.execCommand("Copy");
            document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
            if (lang == "en_US") {
                this.theuser = "Successfully copied"
                this.information = "Successfully copied"
            } else if (lang == "zh_TW") {
                this.theuser = "複製成功"
                this.information = "複製成功"
            } else {
                this.theuser = "复制成功"
                this.information = "复制成功"
            }
            return;
          }
    },
        gotoPromotion(index) {  //推广分页跳转
            if (index == this.Quantity)
                return;
            this.Quantity = index;
        },
        getTime: function (time) { //计算时间
            // var timeStr = time + 28800000;
            if(!time){
                return ''
            }
            var timeStr = time;
            var t = new Date(timeStr) 
            function charLeftAll(n) {
                if (n < 10) return "0" + n;
                else return n;
            }
            return t.getFullYear() + '-' + charLeftAll((t.getMonth() + 1)) + '-' + charLeftAll(t.getDate()) + '  ' +
                charLeftAll(t.getHours()) + ':' + charLeftAll(t.getMinutes()) + ':' + charLeftAll(t.getSeconds())
        },
        Promotiontab(index) { //我的推广
            this.spreadnum = index;
            // if (this.spreadnum == 1)
            //     this.$refs.spreadnav.style.height = "400px"
            // else
            //     this.$refs.spreadnav.style.height = "400px"
        },
        getInvitations(obj=null){ //获取推广用户
            axios.post('/user/myinvitation',obj).then((res) => {
                if(res.data.code==200){
                    this.myinvitation.list=res.data.data.list
                    // this.myinvitation.total=res.data.data.total||0
                    this.myinvitation.total=res.data.data.first.length||0
                }
            })
        },
        getcurrentInvitations(num){ //分页获取推广用户
            this.myinvitation.p=num
            let params={
                p:this.myinvitation.p,
                s:this.myinvitation.s
            }
            this.getInvitations(params)
        },
        getCommision(obj=null){ //获取推广详情
            axios.post('/user/mobile/getCommision',obj).then((res) => {
                if(res.data.code==200){
                   this.usercommision.list=res.data.data.list
                   this.usercommision.total=res.data.data.total
                   this.usercommision.brokerage=res.data.data.data2
                   this.brokerage0=this.usercommision.brokerage.HPS
                   this.brokerage1=this.usercommision.brokerage.ETH
                   this.brokerage2=this.usercommision.brokerage.BTC
                   this.brokerage3=this.usercommision.brokerage.HSR
                   this.brokerage4=this.usercommision.brokerage.HAND 
                   this.brokerage5=this.usercommision.brokerage.HART
                   this.brokerage6=this.usercommision.brokerage.MOT
                }
            })
        },
        getcurrentCommision(num){ //分页获取推广详情
            this.usercommision.p=num
            let params2={
                p:this.usercommision.p,
                s:this.usercommision.s
            }
            this.getCommision(params2)
        }
    }
})